﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ABC_SA_BLV;
using ABC_SA_DM;
using ABC_SA_WEB.WebServiceDestino;

namespace ABC_SA_WEB
{
    public partial class ListarDestinos : System.Web.UI.Page
    {
        wsDestinos servicio = new  wsDestinos();

        protected void Page_Load(object sender, EventArgs e)
        {

            cargarGrid();
        }


        public void cargarGrid() {

            //servicioDestino servicio = new servicioDestino();
            int valor;
            DataTable dt = ConvertToDataTable(servicio.listaDestinos());
            GrdDestino.DataSource = dt;
            if (!Page.IsPostBack)
            {
                //if (Page.PreviousPage == null)
                //    Response.Redirect("../Default.aspx");
                if (Request.QueryString["value1"] != null)
                {
                    valor = Convert.ToInt32(Request.QueryString["value1"]);
                    if (valor == 3)
                        showmsg("El evento ha sido modificado con exito");
                    else
                        showmsg("El evento se agregó con exito");
                }
                GrdDestino.DataBind();
            }
            
        
        
        
        
        }
        protected void BindData()
        {
            wsDestinos servicio = new wsDestinos();
            DataTable dt = ConvertToDataTable(servicio.listaDestinos());
            GrdDestino.DataSource = dt;
            GrdDestino.DataBind();
        }



        protected void GrdDestino_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //    GrdVEventos.DataSource = ServicioTicket.MostrarEventos();
        }

        protected void GrdDestino_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {

        }

   


        public DataTable ConvertToDataTable<T>(List<T> data)
        {
            PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }




        public void showmsg(string str)
        {
            string prompt = "<script>$(document).ready(function(){{$.prompt('{0}!');}});</script>";
            string message = string.Format(prompt, str);
            ClientScript.RegisterStartupScript(typeof(Page), "alert", message);
        }


    


        protected void GrdDestino_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int fila_actualizar = e.RowIndex;
            GridViewRow row = (GridViewRow)GrdDestino.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("Nombre");
            //TextBox txtname=(TextBox)gr.cell[].control[];
            string NombreEvento = ((TextBox)row.Cells[1].Controls[0]).Text;
            //    IEnumerator<DataRowCollection> Collector;
            //     Collector = e.NewValues.Values.GetEnumerator();
            //    String NombreEvento = GrdVEventos.Rows[fila_actualizar].Cells[1].Text.ToString();



            // GrdVEventos.Rows[fila_actualizar].Cells[1].FindControl("NombreEvento").ToString();

            GrdDestino.EditIndex = -1;
            GrdDestino.DataBind();
        }



        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }



        protected void GrdDestino_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GrdDestino.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void GrdDestino_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GrdDestino.EditIndex = e.NewEditIndex;
            GrdDestino.DataBind();
        }


        protected void GrdDestino_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GrdDestino.EditIndex = -1;
            GrdDestino.DataBind();
        }

     


        protected void GrdDestino_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id_destino = GrdDestino.SelectedRow.Cells[2].Text;
            Response.Redirect("DestinosEdit.aspx?value1=" + id_destino);
        }

        protected void btnVolver_Click(object sender, EventArgs e)
        {
            string usuario = (string)(Session["userName"]);
            Session["userName"] = usuario;
            Response.Redirect("PaginaAdmin.aspx?userName");
        }
       
       
    }
}